﻿@using System.Globalization
@using CkSoftware.GroupMe.Sdk.TestWeb.Helpers
@model CkSoftware.GroupMe.Sdk.TestWeb.Models.GroupsListModel

@{
    ViewBag.Title = "List of all groups";
}

<div class="page-header">
    <h1>List of all available groups</h1>
</div>

@if (!SessionConfiguration.IsConfigValueIsSet<string>(SessionConfiguration.GroupMeClientAuthKey))
{
    <h2>Configuration required</h2>
    <p>Before you can start here, you have to configure your GroupMe Access Token @Html.Bootstrap().ActionLink("here", "Index").</p>
}
else
{
    using (var table = Html.Bootstrap().Begin(new Table().Striped()))
    {
        using (table.BeginHeader())
        {
            <tr>
                <th>Group Image</th>
                <th>Id</th>
                <th>Name</th>
                <th>Created at</th>
                <th>Created by UserId</th>
                <th width="200">&nbsp;</th>
            </tr>
        }

        using (var body = table.BeginBody())
        {
            foreach (var group in Model.Groups)
            {
                using (var row = body.BeginRow())
                {
                    @row.Cell(Html.AvatarImageForGroupMePicture(group.Data.Image, group.Data.Name))
                    @row.Cell(group.Data.Id)
                    @row.Cell(group.Data.Name)
                    @row.Cell(string.Concat(group.Data.CreatedAt.ToShortDateString(), ", ", group.Data.CreatedAt.ToShortTimeString()))
                    @row.Cell(group.Data.CreatorUserId)
                    using (row.BeginCell())
                    {
                        using (var buttonGroup = Html.Bootstrap().Begin(new ButtonGroup()))
                        {
                            @buttonGroup.Button().Text(string.Empty).AppendIcon(Icons.list).Tooltip(new Tooltip("Show group details overlay").Placement("top")).TriggerModal(string.Concat("Group", group.Data.Id, "Details"))
                            @buttonGroup.ActionLinkButton(string.Empty, "GroupDetail", "Groups").Tooltip(new Tooltip("Go to group details page").Placement("top")).RouteValues(new { groupId = group.Data.Id }).AppendIcon(Icons.search)
                            @buttonGroup.Button().Text(string.Empty).AppendIcon(Icons.user).Tooltip(new Tooltip("Show group-member info overlay").Placement("top")).TriggerModal(string.Concat("Group", group.Data.Id, "Members"))
                            @buttonGroup.ActionLinkButton(string.Empty, "Update", "Groups").Tooltip(new Tooltip("Edit the group data").Placement("top")).RouteValues(new { groupId = group.Data.Id }).AppendIcon(Icons.edit)
                            @(buttonGroup.ActionLinkButton(string.Empty, "Delete", "Groups")
                                .HtmlAttributes(new { onclick = string.Format(CultureInfo.CurrentCulture, "return confirm('Do you really want to delete the group {0} / {1}?');", Html.Encode(group.Data.Id), Html.Encode(group.Data.Name)) })
                                .Tooltip(new Tooltip("Delete this group")
                                .Placement("top"))
                                .RouteValues(new { groupId = group.Data.Id })
                                .AppendIcon(Icons.remove))
                        }

                        using (var modal = Html.Bootstrap().Begin(new Modal().Id(string.Concat("Group", group.Data.Id, "Details")).Closeable()))
                        {
                            @modal.Header(string.Concat("Details for group '", group.Data.Name, "'"))
                            using (modal.BeginBody())
                            {
                                <dl class="dl-horizontal">
                                    <dt>Id</dt>
                                    <dd>@Html.TextOrDash(group.Data.Id)</dd>
                                    <dt>Name</dt>
                                    <dd>@Html.TextOrDash(group.Data.Name)</dd>
                                    <dt>Created</dt>
                                    <dd>@Html.TextOrDash(group.Data.CreatedAt.ToString(CultureInfo.CurrentCulture))</dd>
                                    <dt>Creator user ID</dt>
                                    <dd>@Html.TextOrDash(group.Data.CreatorUserId)</dd>
                                    <dt>Office mode</dt>
                                    <dd>@Html.TextOrDash(group.Data.OfficeMode.ToString())</dd>
                                    <dt>Last group update</dt>
                                    <dd>@Html.TextOrDash(group.Data.UpdatedAt.ToString(CultureInfo.CurrentCulture))</dd>
                                    <dt>Description</dt>
                                    <dd>@Html.TextOrDash(group.Data.Description)</dd>
                                    <dt>Messages count</dt>
                                    <dd>@Html.TextOrDash(group.Data.Messages.Count.ToString(CultureInfo.CurrentCulture))</dd>
                                    <dt>Last message ID</dt>
                                    <dd>@Html.TextOrDash(group.Data.Messages.LastMessageId)</dd>
                                    <dt>Last msg created at</dt>
                                    <dd>@Html.TextOrDash(group.Data.Messages.LastMessageCreatedAt.ToString(CultureInfo.CurrentCulture))</dd>
                                    <dt>Preview nickname</dt>
                                    <dd>@Html.TextOrDash(group.Data.Messages.Preview.Nickname)</dd>
                                    <dt>Preview text</dt>
                                    <dd>@Html.TextOrDash(group.Data.Messages.Preview.Text)</dd>
                                    <dt>Share URL</dt>
                                    <dd>@Html.TextOrDash(group.Data.ShareUrl)</dd>
                                </dl>
                            }
                            using (modal.BeginFooter())
                            {}
                        }
                        using (var modal = Html.Bootstrap().Begin(new Modal().Id(string.Concat("Group", group.Data.Id, "Members")).Closeable()))
                        {
                            @modal.Header(string.Concat("'", group.Data.Name, "' Members"))
                            using (modal.BeginBody())
                            {
                                using(var memberTable = Html.Bootstrap().Begin(new Table().Striped()))
                                {
                                    using (memberTable.BeginHeader())
                                    {
                                        <tr>
                                            <th>Avatar</th>
                                            <th>Id</th>
                                            <th>UserId</th>
                                            <th>Nickname</th>
                                            <th>Muted</th>
                                            <th>Auto-Kicked</th>
                                        </tr>
                                    }
                                    using (var memberBody = memberTable.BeginBody())
                                    {
                                        foreach (var member in group.Data.Members)
                                        {
                                            using (var memberRow = memberBody.BeginRow())
                                            {
                                                @memberRow.Cell(Html.AvatarImageForGroupMePicture(member.AvatarImage, member.Nickname))
                                                @memberRow.Cell(member.Id)
                                                @memberRow.Cell(member.UserId)
                                                @memberRow.Cell(member.Nickname)
                                                @memberRow.Cell(Html.Bootstrap().Icon(member.Muted ? Icons.ok : Icons.remove))
                                                @memberRow.Cell(Html.Bootstrap().Icon(member.AutoKicked ? Icons.ok : Icons.remove))
                                            }
                                        }
                                    }
                                }
                            }
                            using (modal.BeginFooter())
                            {}
                        }
                    }
                }
            }
        }
    }
}